package com.crm.service.impl;


import com.crm.dto.ActivityDTO;

import com.crm.dto.AddUpActivityDTO;

import com.crm.mapper.ActivityMapper;
import com.crm.pojo.Activity;
import com.crm.result.PageResult;
import com.crm.result.Result_code;
import com.crm.service.ActivityService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Random;

/**
 * 活动管理 业务逻辑层
 */
@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 分页查询活动并返回带统计信息的结果
     *
     * @return
     */
    @Override
    public PageResult getActivity(ActivityDTO dto) {
        LocalDate beginTime = dto.getBeginTime();
        LocalDate endTime = dto.getEndTime();
        LocalDate beginCreateTime = dto.getBeginCreateTime();
        LocalDate endCreateTime = dto.getEndCreateTime();
        //调用分页工具
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        //动态sql查询
        List<Activity> list = activityMapper.getActivity(dto);
        Page<Activity> page = (Page<Activity>) list;

        //封装分页结果
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());

        log.info("活动返回分页结果：{}", pageResult);
        return pageResult;
    }


    /**
     * 添加活动
     *
     * @param dto
     */
    @Override
    public void addActivity(AddUpActivityDTO dto) {
        Activity activity = activityNewCopye(dto);

        activityMapper.addActivity(activity);

    }


    //更新活动
    @Override
    public Result_code update(AddUpActivityDTO dto) {
        Activity activity = activityNew(dto);
        int activityNew = activityMapper.updateActivity(activity);
        if (activityNew == 0) {
            return Result_code.error("活动更新失败或未找到该活动");
        }
        return Result_code.success("更新成功");
    }

    // 根据id 查活动
    @Override
    public Activity getByid(Integer id) {
        return activityMapper.getByid(id);
    }


    //删除活动
    @Override
    public Result_code delete(Integer id) {
        log.info("接到的参数是{}", id);
        activityMapper.delete(id);
        return Result_code.success("删除成功");

    }

    /**
     * 方法
     * 解析 AddUpActivityDTO请求类
     *
     * @param dto
     * @return
     */
    public Activity activityNew(AddUpActivityDTO dto) {
        Activity activity1 = new Activity();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 解析字符串时间为 LocalDateTime
        String beginTime = dto.getBeginTime();
        String endTime = dto.getEndTime();
        LocalDateTime beginTimeNew = LocalDateTime.parse(beginTime, formatter);
        LocalDateTime endTimeNew = LocalDateTime.parse(endTime, formatter);

        // 复制其他属性
        BeanUtils.copyProperties(dto, activity1);
        activity1.setEndTime(endTimeNew);
        activity1.setBeginTime(beginTimeNew);
        activity1.setCreateTime(LocalDateTime.now());
        activity1.setUpdateTime(LocalDateTime.now());
        return activity1;
    }
    public Activity activityNewCopye(AddUpActivityDTO dto) {
        Activity activity1 = new Activity();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

        // 解析字符串时间为 LocalDateTime
        String beginTime = dto.getBeginTime();
        String endTime = dto.getEndTime();
        LocalDateTime beginTimeNew = LocalDateTime.parse(beginTime, formatter);
        LocalDateTime endTimeNew = LocalDateTime.parse(endTime, formatter);
        Random RANDOM = new Random();
        String code = String.valueOf(1000 + RANDOM.nextInt(9000));

        activity1.setCode(code);
        // 复制其他属性
        BeanUtils.copyProperties(dto, activity1);
        activity1.setEndTime(endTimeNew);
        activity1.setBeginTime(beginTimeNew);
        activity1.setCreateTime(LocalDateTime.now());
        activity1.setUpdateTime(LocalDateTime.now());
        return activity1;
    }
    @Override
    public List<Activity> queryList(String channel) {
        return activityMapper.queryList(channel);
    }
}